<?php
namespace app\common\model;
class Customer extends Base{
    function lists(){
        $module=model('artmodule')->read(['name'=>'single'],'id');
        // $list=model('member')
        // ->field('m.id,m.tel,m.name,m.head_img,ifnull(c.s_time,0) as s_time,c.message')
        // ->alias('m')
        // ->where([
        //     'm.status'=>1,
        //     'm.module_id'=>$module['id']
        // ])
        // ->leftJoin('customer c','c.uid=m.id or c.relation_id=m.id')
        // ->order('c.s_time desc')
        // ->group('m.id')
        // ->select()->toArray();
        $field='m.id,m.tel,m.name,m.head_img,m.is_top,ifnull(c.s_time,0) as s_time,c.message,ifnull(sum(c.is_read),0) as notReads';
        $table=model('member')->getTable();
        $module_id=$module['id'];

        $customer=$this->getTable();
        $childSql="(select * from {$customer} order by s_time desc)";
        $sql="select {$field} from $table as m  left join {$childSql} as c on c.uid=m.id or c.relation_id=m.id where m.status=1 and m.module_id={$module_id} group by m.id order by m.is_top desc,s_time desc";
        $list=$this->query($sql);
        return $list;
    }
    /**
     * 写入信息
     * @param  integer $uid    写入的用户
     * @param  string  $message    信息内容
     * @param  integer $isCustomer 0:客户写入 1：客服回复的用户ID
     * @return array
     */
    function push($uid,$message,$isCustomer=0){
        if($isCustomer>0){
            // 判断是否客服人员
        }
        $data=[
            'uid'=>$uid,
            'message'=>$message,
            's_time'=>time(),
            'relation_id'=>$isCustomer,
            'is_read'=>$isCustomer>0?0:1,
        ];
        $add=$this->add($data);
        if(empty($add)){
            return false;
        }
        // 如果是客服回复应该做一些推送
        if($isCustomer>0){
            $t=model('member')->read(['id'=>$isCustomer],'tel,head_img');
            if(isset($t['tel'])){
                $head_img=empty($t['head_img'])?'':images($t['head_img']);
                $message=[
                    'id'=>$add['id'],
                    'uid'=>$data['uid'],
                    's_time'=>$data['s_time'],
                    'message'=>$data['message'],
                    'headImage'=>$head_img,
                ];
                $this->jpush($message,$this->getAudience($t['tel']));
            }
        }
        // 统一新消息给客服人员
        \socket\Socket::admin([
            'event'=>'customer_push_message',
            'data'=>$add,
            'counts'=>$isCustomer>0?0:1,
        ]);
        return $add;
    }
    /**
     * 客户端拉取信息记录
     * @param  integer $uid    用户ID
     * @param  integer $s_time 起始时间戳
     * @param  integer $e_time 结束时间戳
     * @return array
     */
    function pull($uid,$s_time=0,$e_time=0,$admin_id=0){
        if($e_time==0){
            $e_time=strtotime(date('Y-m-d'))+86400;
        }
        // 判断是否客服人员
        // 非客服人员获取记录方式
        $map=[
            'uid|relation_id'=>$uid,
            's_time'=>['s_time','between',[$s_time,$e_time]],
        ];
        $options=[
            'order'=>'s_time desc',
        ];
        $list=$this->readAll($map,30,$options)->toArray();
        $list=$list['data'];
        usort($list, function($a,$b){
            return $a['s_time']>$b['s_time'];
        });
        if($admin_id!=$uid){
            $e=$this->edit(['is_read'=>0],[
                'uid|relation_id'=>$uid,
            ]);
            if($e){
                // 统一各个客服去红点操作
                \socket\Socket::admin([
                    'event'=>'customer_readed',
                    'uid'=>$uid,
                    'counts'=>0-$e,
                ]);
            }
        }
        
        return $list;
    }
    /**
     * 推送自定义消息(暂未做异常处理)
     * @param  array|string $message 推送的自定义内容
     * @return void
     */
    function jpush($message,$audience){
        if(is_array($message)){
            $message=json_encode($message);
        }
        $app_key = config('config.jpush_app_key');
        $master_secret = config('config.jpush_master_secret');
        $c=new \JPush\Client($app_key, $master_secret);
        $c->push()
            ->setPlatform('all')
            ->message($message)
            ->addAlias($audience)
            ->send();

    }
    /**
     * 获取当前手机号码的极光推送别名
     * @param  integer|string $tel 手机号码
     * @return string
     */
    function getAudience($tel){
        return strtoupper(md5($tel.'CG2018cg'));
    }
}
